home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / dtype / FontDT_1_0.lha / Src / otag.c < prev    next >
C/C++ Source or Header  |  1995-04-22  |  4KB  |  145 lines

  1. /*
  2. **    otag.c - .otag files reading routines
  3. **    Copyright © 1995 Michael Letowski
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <dos/dos.h>
  8. #include <diskfont/diskfont.h>
  9. #include <diskfont/diskfonttag.h>
  10. #include <support/types.h>
  11.  
  12. #include <proto/exec.h>
  13. #include <proto/dos.h>
  14. #include <proto/diskfont.h>
  15. #include <proto/utility.h>
  16.  
  17. #include "classbase.h"
  18. #include "otag.h"
  19.  
  20. #define FTSUFFIX                        ".font"
  21.  
  22. #define SUFFIX_LEN                    5
  23. #define MAX_NAME_LEN                32
  24.  
  25. /* Get size of FontContentHeader */
  26. #define SizeOfFCH(fch)            (sizeof(struct FontContentsHeader)+\
  27.                                                             (fch)->fch_NumEntries*sizeof(struct FontContents))
  28. #define VarSizeOfFCH(x)            (sizeof(struct FontContentsHeader)+\
  29.                                                             (x)*sizeof(struct FontContents))
  30.  
  31. STATIC struct FontContentsHeader *ReadOutlines(struct ClassBase *cb,
  32.                                                                                                 BPTR lock, STRPTR name);
  33. STATIC LONG SeekFileSize(struct ClassBase *cb, BPTR fh);
  34.  
  35. /****************************************************************************/
  36. /*    These functions are necessary, because Diskfont doesn't work well     */
  37. /****************************************************************************/
  38.  
  39. struct FontContentsHeader *NewFC(struct ClassBase *cb, BPTR lock, STRPTR name)
  40. {
  41.     struct FontContentsHeader *FCH,*MyFCH=NULL;
  42.     ULONG Size;
  43.  
  44.     if(FCH=NewFontContents(lock,name))                        /* Use Diskfont to get contents */
  45.     {
  46.         if(FCH->fch_NumEntries)                                            /* Some entries exist */
  47.         {
  48.             Size=SizeOfFCH(FCH);
  49.             if(MyFCH=AllocVec(Size,MEMF_CLEAR))                /* Allocate new space */
  50.                 CopyMem(FCH,MyFCH,Size);                                /* Make a copy */
  51.         }
  52.         else if(FCH->fch_FileID=OFCH_ID)                        /* This is outline font */
  53.         {
  54.             if(MyFCH=ReadOutlines(cb,lock,name))            /* Read tags and sizes */
  55.                 MyFCH->fch_FileID=FCH->fch_FileID;
  56.         }
  57.         DisposeFontContents(FCH);                                        /* Free unused header */
  58.     }
  59.     return(MyFCH);
  60. }    /* NewFC */
  61.  
  62. VOID DisposeFC(struct ClassBase *cb, struct FontContentsHeader *fch)
  63. {
  64.     FreeVec(fch);
  65. }    /* DisposeFC */
  66.  
  67. STATIC struct FontContentsHeader *ReadOutlines(struct ClassBase *cb,
  68.                                                                                                 BPTR lock, STRPTR name)
  69. {
  70.     struct FontContentsHeader *FCH=NULL;
  71.     struct TFontContents *TFC;
  72.     struct TagItem *Tags;
  73.     struct FileInfoBlock *FIB;
  74.     UWORD *Sizes;
  75.     CHAR NewName[MAX_NAME_LEN];
  76.     STRPTR Suffix;
  77.     BPTR FH,OldLock;
  78.     ULONG FileLen,Offset,I;
  79.  
  80.     OldLock=CurrentDir(lock);                                            /* Change to FONTS: dir */
  81.  
  82.     /* Chage extension */
  83.     clear(&NewName);                                                            /* Set to NULLs */
  84.     strncpy(NewName,name,MAX_NAME_LEN-1);                    /* Copy name to buffer */
  85.     unless(Suffix=strstr(NewName,FTSUFFIX))
  86.     {
  87.         SetIoErr(ERROR_OBJECT_WRONG_TYPE);
  88.         throw(EXIT);
  89.     }
  90.     strncpy(Suffix,OTSUFFIX,SUFFIX_LEN);                    /* Replace ".font" with ".otag" */
  91.  
  92.     /* Open .otag file and get its size */
  93.     try(FH=Open(NewName,MODE_OLDFILE),                        NO_FILE);
  94.     if(FIB=AllocDosObject(DOS_FIB,NULL))
  95.     {
  96.         if(ExamineFH(FH,FIB))                                                /* Examine it */
  97.             FileLen=FIB->fib_Size;                                        /* Get length of file */
  98.         else
  99.             FileLen=SeekFileSize(cb,FH);                            /* Get size by seeking */
  100.         FreeDosObject(DOS_FIB,FIB);
  101.     }
  102.     else
  103.         FileLen=SeekFileSize(cb,FH);                                /* No FIB? - seek */
  104.  
  105.     try(FileLen>=0,    ERROR1);                                            /* Make sure size is positive */
  106.     try(Tags=AllocVec(FileLen,MEMF_ANY),                    NO_TAGS);    /* Allocate space for tags */
  107.     try(Read(FH,Tags,FileLen)==FileLen,                        ERROR2);    /* Read tags */
  108.     try(GetTagData(OT_FileIdent,0,Tags)==FileLen,    ERROR2);    /* Verify size */
  109.  
  110.     /* Get size of new FCH and set up data */
  111.     try(Offset=GetTagData(OT_AvailSizes,0,Tags),    ERROR2);
  112.     Sizes=(UWORD *)((ULONG)Tags+Offset);                    /* Calculate position of data */
  113.     if(FCH=AllocVec(VarSizeOfFCH(*Sizes),MEMF_CLEAR))
  114.     {
  115.         FCH->fch_NumEntries=*Sizes;
  116.         for(I=0; I<*Sizes; I++)
  117.         {
  118.             TFC=&TFontContents(FCH)[I];
  119.             SNPrintf(TFC->tfc_FileName,MAXFONTPATH,"%s/%ld",name,Sizes[I+1]);
  120.             TFC->tfc_TagCount=1;                                            /* TAG_DONE */
  121.             TFC->tfc_YSize=Sizes[I+1];
  122.             TFC->tfc_Style=FS_NORMAL;                                    /* Really ??? */
  123.             TFC->tfc_Flags=FPF_DISKFONT;                            /* Really ??? */
  124.         }
  125.     }
  126.  
  127.     except(ERROR2,    );
  128.     except(NO_TAGS,    FreeVec(Tags));
  129.     except(ERROR1,    );
  130.     except(NO_FILE,    Close(FH));                                        /* Close opened .otag file */
  131.     except(EXIT,        );
  132.  
  133.     CurrentDir(OldLock);                                                    /* Get back to old dir */
  134.     return(FCH);
  135. }    /* ReadOutlines */
  136.  
  137. STATIC LONG SeekFileSize(struct ClassBase *cb, BPTR fh)
  138. {
  139.     LONG Size;
  140.  
  141.     if((Size=Seek(fh,0,OFFSET_END))>=0)                        /* Seek to end of file */
  142.         Size=Seek(fh,0,OFFSET_BEGINNING);                        /* Seek back to beginning */
  143.     return(Size);
  144. }    /* SeekFileSize */
  145.